<template>
  <div>
    <el-card class="search-card">
      <div class="class_70">
        <el-row>
          <el-col :span="24" class="tac">
            {{ queryParams.diningTime }}
            订餐数量统计 （团餐计划）
          </el-col>
        </el-row>
        <el-row :gutter="20" class="margin_top_20 fs_14">
          <el-col :span="8">
            <el-card class="search-card">
              <div>早餐：{{ breakfastObj.breakfastObj_3 }}</div>
              <div class="dis aic margin_top_20 padding_bottom_20">
                <div class="flex_1">普餐：{{ breakfastObj.breakfastObj_1 }}</div>
                <div class="flex_1">清餐：{{ breakfastObj.breakfastObj_2 }}</div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card class="search-card">
              <div>午餐：{{ dinnerObj.dinnerObj_3 }}</div>
              <div class="dis aic margin_top_20 padding_bottom_20">
                <div class="flex_1">普餐：{{ dinnerObj.dinnerObj_1 }}</div>
                <div class="flex_1">清餐：{{ dinnerObj.dinnerObj_2 }}</div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card class="search-card">
              <div>晚餐：{{ lunchObj.lunchObj_3 }}</div>
              <div class="dis aic margin_top_20 padding_bottom_20">
                <div class="flex_1">普餐：{{ lunchObj.lunchObj_1 }}</div>
                <div class="flex_1">清餐：{{ lunchObj.lunchObj_2 }}</div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </el-card>
    <el-card class="search-card">
      <el-form
        size="small"
        :model="queryParams"
        ref="queryParams"
        class="formBox"
        label-width="88px"
      >
        <el-row :gutter="0">
          <el-col :span="5">
            <el-form-item label="年级" prop="gradeId">
              <el-select
                v-model="queryParams.gradeId"
                placeholder="请选择年级名称"
                style="width: 100%"
                @change="gradeFun"
              >
                <el-option
                  v-for="(item, index) in gradeList"
                  :key="index"
                  :value="item.id"
                  :label="item.gradeName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="班级" prop="classId">
              <el-select
                v-model="queryParams.classId"
                placeholder="请选择班级"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in classList"
                  :key="index"
                  :value="item.id"
                  :label="item.className"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="日期" prop="diningTime">
              <el-date-picker
                v-model="queryParams.diningTime"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item>
              <div class="dis aic">
                <div class="flex_1"></div>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                >
                  搜索
                </el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
                  重置
                </el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="search-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            >导出</el-button
          >
        </el-col>
        <right-toolbar @queryTable="getList"></right-toolbar>
      </el-row>
    </el-card>
    <el-card class="table-card">
      <el-table :data="dataList">
        <el-table-column label="日期" align="center" prop="diningTime" />
        <!-- <el-table-column label="校区" align="center" prop="idCardNumber" /> -->
        <el-table-column label="年级" align="center" prop="gradeName" />
        <el-table-column label="班级" align="center" prop="className" />
        <el-table-column label="早餐总数" align="center" prop="breakfastCount" />
        <el-table-column
          label="早（普餐）"
          align="center"
          prop="ordinaryBreakfastCount"
        />
        <el-table-column label="早（清真）" align="center" prop="halalBreakfastCount" />
        <el-table-column label="午餐总数" align="center" prop="lunchCount" />
        <el-table-column label="午（普餐）" align="center" prop="ordinaryLunchCount" />
        <el-table-column label="午（清真）" align="center" prop="halalLunchCount" />
        <el-table-column label="晚餐总数" align="center" prop="dinnerCount" />
        <el-table-column label="晚（普餐）" align="center" prop="ordinaryDinnerCount" />
        <el-table-column label="晚（清真）" align="center" prop="halalDinnerCount" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="buttonMethods(1, scope.row)"
              v-hasPermi="['campus:grade:edit']"
              >明细详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <el-dialog :title="title" :visible.sync="status" width="50%" append-to-body>
      <el-row :gutter="10">
        <!-- <el-col :span="4">校区：{{OrderDetailListValue.gradeName}}</el-col> -->
        <el-col :span="4">年级：{{ OrderDetailListValue.gradeName }} </el-col>
        <el-col :span="4">班级：{{ OrderDetailListValue.className }} </el-col>
        <el-col :span="13">时间：{{ OrderDetailListValue.diningTime }} </el-col>
        <el-col :span="2">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="buttonMethods(2)"
            >导出
          </el-button>
        </el-col>
      </el-row>
      <el-table :data="OrderDetailList" class="margin_top_20">
        <!-- <el-table-column label="序号" align="center" prop="diningTime" /> -->
        <el-table-column label="姓名" align="center" prop="personName" />
        <el-table-column label="身份证号" align="center" prop="idNumber" />
        <el-table-column label="早餐" align="center" prop="breakfast" />
        <el-table-column label="午餐" align="center" prop="lunch" />
        <el-table-column label="晚餐" align="center" prop="dinner" />
      </el-table>
    </el-dialog>
    <!-- status -->
  </div>
</template>
<script>
import { listGrade } from "@/api/campus/grade";
import { listClassManage } from "@/api/campus/classManage";
import {
  selectTeamDiningOrder,
  countTeamDiningOrder,
  selectTeamDiningOrderDetail,
} from "@/api/teamMeal/order";

export default {
  name: "teamMealOrder",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 1,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      status: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gradeId: null,
        classId: null,
        diningTime: [],
      },
      dataList: [],
      campusList: [],
      gradeList: [],
      classList: [],
      queryForm: {},
      breakfastObj: {},
      dinnerObj: {},
      lunchObj: {},

      OrderDetailList: [],
      OrderDetailListValue: {},
    };
  },
  watch: {
    "queryParams.diningTime": function (newVal) {
      if (newVal) {
        this.getList();
        this.countTeamDiningOrderFun();
      }
    },
  },
  created() {
    var time = new Date();
    var year = time.getFullYear();
    var month =
      time.getMonth() + 1 < 10 ? "0" + (time.getMonth() + 1) : time.getMonth() + 1;
    var day = time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
    this.queryParams.diningTime = year + "-" + month + "-" + day;
    this.listGradeFun();
    this.getList();
    this.countTeamDiningOrderFun();
  },
  activated() {
    this.listGradeFun();
    this.getList();
    this.countTeamDiningOrderFun();
    this.$forceUpdate();
  },
  methods: {
    // 列表
    getList() {
      selectTeamDiningOrder(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.dataList = res.rows;
          this.total = res.total;
        }
      });
    },
    countTeamDiningOrderFun() {
      countTeamDiningOrder(this.queryParams).then((res) => {
        if (res.code == 200) {
          var breakfast = res.data.breakfast;
          var breakfastObj = {
            breakfastObj_1: 0,
            breakfastObj_2: 0,
            breakfastObj_3: 0,
          };
          breakfast.forEach((item) => {
            if (item.diningStyle == "1") {
              breakfastObj.breakfastObj_1 = item.totalPerson;
            } else if (item.diningStyle == "2") {
              breakfastObj.breakfastObj_2 = item.totalPerson;
            } else if (item.diningStyle == "3") {
              breakfastObj.breakfastObj_3 = item.totalPerson;
            }
          });
          this.breakfastObj = breakfastObj;

          var dinner = res.data.dinner;
          var dinnerObj = {
            dinnerObj_1: 0,
            dinnerObj_2: 0,
            dinnerObj_3: 0,
          };
          dinner.forEach((item) => {
            if (item.diningStyle == "1") {
              dinnerObj.dinnerObj_1 = item.totalPerson;
            } else if (item.diningStyle == "2") {
              dinnerObj.dinnerObj_2 = item.totalPerson;
            } else if (item.diningStyle == "3") {
              dinnerObj.dinnerObj_3 = item.totalPerson;
            }
          });
          this.dinnerObj = dinnerObj;

          var lunch = res.data.lunch;
          var lunchObj = {
            lunchObj_1: 0,
            lunchObj_2: 0,
            lunchObj_3: 0,
          };
          lunch.forEach((item) => {
            if (item.diningStyle == "1") {
              lunchObj.lunchObj_1 = item.totalPerson;
            } else if (item.diningStyle == "2") {
              lunchObj.lunchObj_2 = item.totalPerson;
            } else if (item.diningStyle == "3") {
              lunchObj.lunchObj_3 = item.totalPerson;
            }
          });
          this.lunchObj = lunchObj;
        }
      });
    },
    /**
     * 获取成绩列表
     * @param options 包含校区ID的对象
     */
    listGradeFun(options) {
      console.log(this);
      listGrade({ pageNum: 1, pageSize: 200, campusId: options }).then((res) => {
        if (res.code == 200) {
          this.gradeList = res.rows;
        }
      });
    },
    /**
     * 获取班级列表
     * @param options - 年级ID
     */
    gradeFun(options) {
      listClassManage({ pageNum: 1, pageSize: 200, gradeId: options }).then(
        (response) => {
          this.classList = response.rows;
        }
      );
    },
    /**
     * 按钮相关的方法集合
     *
     * @returns {void}
     */
    buttonMethods(options, value) {
      // 添加
      if (options == 1) {
        this.title = "明细详情";
        selectTeamDiningOrderDetail({
          campusId: value.campusId,
          classId: value.classId,
          gradeId: value.gradeId,
          diningTime: value.diningTime,
        }).then((res) => {
          this.OrderDetailList = res.data;
          this.OrderDetailListValue = value;
          this.status = true;
        });
      } else if (options == 2) {
        this.download(
          "/diningOrder/exportTeamDiningOrderDetail",
          {
            ...this.OrderDetailListValue,
          },
          `团餐订单明细导出.xlsx`
        );
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.handleQuery();
      this.classList = [];
      var time = new Date();
      var year = time.getFullYear();
      var month =
        time.getMonth() + 1 < 10 ? "0" + (time.getMonth() + 1) : time.getMonth() + 1;
      var day = time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
      this.queryParams.diningTime = year + "-" + month + "-" + day;
    },
    // 导出
    handleExport() {
      this.download(
        "/diningOrder/exportTeamDiningOrder",
        {
          ...this.queryParams,
        },
        `团餐订单导出.xlsx`
      );
    },
  },
};
</script>
<style>
.formBox >>> .el-form-item--small.el-form-item {
  margin-bottom: 0px !important;
}

.formBox >>> .el-form-item__label {
  font-size: 12px !important;
  padding: 0 !important;
}
.class_70 {
  width: 70%;
  margin: 0 auto;
}
</style>
